const app = getApp()
Page({
  data: {
    _id: '',
    ktxq: '',
    disabled: true,
    focus: false,
    ktmc: '',
    ktjj: '',
    jsyq: '',
    ckzl: '',
    ktzt: -1,
    bjSave: true,
    sqrs: ''
  },
  onLoad: function(e) {
    this.setData({
      _id: e._id
    })
  },
  onShow: function() {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Topic').doc(that.data._id).get().then(res => {
      that.setData({
        ktxq: res.data,
        ktmc: res.data.ktmc,
        ktjj: res.data.ktjj,
        jsyq: res.data.jsyq,
        ckzl: res.data.ckzl,
        ktzt: res.data.ktzt
      })
    })
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Option').where({
      topicId: that.data._id
    }).count().then(res => {
      that.setData({
        sqrs: res.total
      })
    })
  },
  inpKtmc: function(e) {
    this.setData({
      ktmc: e.detail.value
    })
  },
  inpKtjj: function(e) {
    this.setData({
      ktjj: e.detail.value
    })
  },
  inpJsyq: function(e) {
    this.setData({
      jsyq: e.detail.value
    })
  },
  inpCkzl: function(e) {
    this.setData({
      ckzl: e.detail.value
    })
  },
  funBj: function() {
    this.setData({
      disabled: false,
      focus: true,
      bjSave: false
    })
  },
  funSave: function() {
    const that = this
    that.setData({
      disabled: true,
      focus: false
    })
    wx.cloud.callFunction({
      name: 'topicUpdate',
      data: {
        _id: that.data._id,
        ktmc: that.data.ktmc,
        ktjj: that.data.ktjj,
        jsyq: that.data.jsyq,
        ckzl: that.data.ckzl
      }
    }).then(res => {
      if (res.result.result == 1) {
        wx.showToast({
          title: '课题修改成功！',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          bjSave: true
        })
      } else {
        wx.showToast({
          title: '课题修改失败！',
          icon: 'node',
          duration: 2000
        })
      }
    })
  },
  funApplist: function() {
    if (this.data.sqrs == 0 || this.data.sqrs == '') {
      wx.showToast({
        title: '此课题还没有学生申请',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: '../teaAppstu/teaAppstu?_id=' + this.data._id + '&ktmc=' + this.data.ktxq.ktmc
      })
    }
  },
  funDelete: function() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确认删除课题？',
      cancelColor: '#000000',
      cancelText: '确认',
      confirmColor: '#576B95',
      confirmText: '取消',
      success(res) {
        if (res.confirm) {} else if (res.cancel) {
          wx.cloud.callFunction({
            name: 'topicDelete',
            data: {
              _id: that.data._id,
              ktzt: that.data.ktzt,
              sendId: app.globalData.userId,
              sendName: app.globalData.userName,
              ktmc: that.data.ktxq.ktmc
            }
          }).then(res => {
            wx.showToast({
              title: '课题删除成功！',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 2500)
          })
        }
      }
    })
  },
  zdryxx: function() {
    wx.navigateTo({
      url: '../teaStuxx/teaStuxx?stuId=' + this.data.ktxq.stuId + '&from=ktgl'
    })
  }
})